--[[

gui.lua

This lua file handles the GUI options screen functions.

]]

local function LKC_GUI_AddDescLeft(myFrame, myText)
	local leftDesc = myFrame:CreateFontString("leftDesc");
	leftDesc:ClearAllPoints();
	leftDesc:SetFontObject(GameFontNormal);
	leftDesc:SetJustifyH("RIGHT");
	leftDesc:SetJustifyV("TOP");
	leftDesc:SetPoint("TOPRIGHT", myFrame, "TOPLEFT", -10, -9);
	leftDesc:SetText(myText);
	leftDesc:Show();
end

local function LKC_GUI_SetEditBoxDetails(myFrame, myText)
	-- Set the Edit Box Size
	myFrame:SetSize(250, 32);
	myFrame:SetMaxBytes(128);
	
	-- Disable Frame auto-focus
	myFrame:SetAutoFocus(false);

	-- Show the frame
	myFrame:Show();
	myFrame:ClearFocus();

	-- Set "blank" text
	myFrame:SetText("-");
end


-- Function: Fill the data in the GUI config screen
function LKC_GUI_FillData()
	-- Show the Prefix Values
	local prefixFrame = _G["LKC_GUI_PREFIX"];
	if(LK_PREFIX) then
		prefixFrame:SetText(LK_PREFIX);
	else
		prefixFrame:SetText("");
	end

	-- Show the Emote Prefix Value
	local emoteFrame = _G["LKC_GUI_EMPREFIX"];
	if(LK_EMOTEPREFIX) then
		emoteFrame:SetText(LK_EMOTEPREFIX);
	else
		emoteFrame:SetText("");
	end

	-- Display Substitution Texts
	for index=1,10 do
		local guiFrame = _G["LKC_GUI_SUB"..index];
		if (LK_SUBS[index]) then
			guiFrame:SetText(LK_SUBS[index]);
		else
			guiFrame:SetText("");
		end
	end

	-- Show the Secret Codes
	for index=1,3 do
		local codeFrame = _G["LKC_GUI_SECRET"..index];
		if (codeFrame) then
			if (LK_SECRET[index]) then
				codeFrame:SetText(LK_SECRET[index]);
			end
		end
	end

end

-- Function: What to do when the config "Save" button is clicked.
function LKC_GUI_SaveButtonClick()
	-- Get the new chat prefix
	local prefixFrame = _G["LKC_GUI_PREFIX"];
	LK_PREFIX	= prefixFrame:GetText();

	-- Get the new emote prefix
	local emoteFrame = _G["LKC_GUI_EMPREFIX"];
	LK_EMOTEPREFIX	= emoteFrame:GetText();

	-- Get the secret codes
	for index=1,3 do
		local codeFrame = _G["LKC_GUI_SECRET"..index];
		if (codeFrame) then
			LK_SECRET[index]	= codeFrame:GetText();
		end
	end

	-- Display Substitution Texts
	for index=1,10 do
		local guiFrame = _G["LKC_GUI_SUB"..index];
		LK_SUBS[index] = guiFrame:GetText();
	end
end

-- Function: Create the actual GUI Config Screen
function LKC_RegConfig()
	-- Create main frame for information text
	local LKC_GUI	= CreateFrame("FRAME", "LKC_GUI");
	LKC_GUI.name	= LK_NAME;
	InterfaceOptions_AddCategory(LKC_GUI);

	-- Options Header
	local LKC_GUI_Header = LKC_GUI:CreateFontString(nil, "OVERLAY");
	LKC_GUI_Header:SetFontObject(GameFontNormalLarge);
	LKC_GUI_Header:SetJustifyH("LEFT");
	LKC_GUI_Header:SetJustifyV("TOP");
	LKC_GUI_Header:ClearAllPoints();
	LKC_GUI_Header:SetPoint("TOPLEFT", 10, -10);
	LKC_GUI_Header:SetText(LK_NAME);

	-- Version
	local LKC_GUI_Version = LKC_GUI:CreateFontString(nil, "OVERLAY")
	LKC_GUI_Version:SetFontObject(GameFontNormalLarge)
	LKC_GUI_Version:SetJustifyH("LEFT")
	LKC_GUI_Version:SetJustifyV("TOP")
	LKC_GUI_Version:ClearAllPoints()
	LKC_GUI_Version:SetPoint("TOPLEFT", LKC_GUI_Header,"BOTTOMLEFT", 0, 0)
	LKC_GUI_Version:SetText("Version "..LK_VERSION)

	-- Chat Prefix
	local LKC_GUI_PREFIX = CreateFrame("EditBox", "LKC_GUI_PREFIX", LKC_GUI, "InputBoxTemplate");
	LKC_GUI_PREFIX:SetPoint("TOPLEFT", LKC_GUI, "TOPLEFT", 170, -70);
	LKC_GUI_SetEditBoxDetails(LKC_GUI_PREFIX);
	LKC_GUI_AddDescLeft(LKC_GUI_PREFIX, "Chat Prefix -");

	-- Emote Prefix
	local LKC_GUI_EMPREFIX = CreateFrame("EditBox", "LKC_GUI_EMPREFIX", LKC_GUI, "InputBoxTemplate");
	LKC_GUI_EMPREFIX:SetPoint("TOPLEFT", LKC_GUI_PREFIX, "TOPLEFT", 0, -30);
	LKC_GUI_SetEditBoxDetails(LKC_GUI_EMPREFIX);
	LKC_GUI_AddDescLeft(LKC_GUI_EMPREFIX, "Emote Prefix -");

	-- Substitution String 1
	local LKC_GUI_SUB1 = CreateFrame("EditBox", "LKC_GUI_SUB1", LKC_GUI, "InputBoxTemplate");
	LKC_GUI_SUB1:SetPoint("TOPLEFT", LKC_GUI_EMPREFIX, "TOPLEFT", 0, -50);
	LKC_GUI_SetEditBoxDetails(LKC_GUI_SUB1);
	LKC_GUI_AddDescLeft(LKC_GUI_SUB1, "Substitution String 1 -");

	-- Display the rest of the substitution strings
	for index=2,10 do
		local editBox = CreateFrame("EditBox", "LKC_GUI_SUB"..index, LKC_GUI, "InputBoxTemplate");
		editBox:SetPoint("TOPLEFT", "LKC_GUI_SUB"..(index - 1), "TOPLEFT", 0, -30);
		LKC_GUI_SetEditBoxDetails(editBox);
		LKC_GUI_AddDescLeft(editBox, "Substitution String "..index.." -");
	end

	-- Display the secret code string boxes
	local codeBox = CreateFrame("EditBox", "LKC_GUI_SECRET1", LKC_GUI, "InputBoxTemplate");
	codeBox:SetPoint("TOPLEFT", "LKC_GUI_SUB10", "TOPLEFT", 0, -50);
	LKC_GUI_SetEditBoxDetails(codeBox);
	LKC_GUI_AddDescLeft(codeBox, "My Secret Code -");

	local codeBox = CreateFrame("EditBox", "LKC_GUI_SECRET2", LKC_GUI, "InputBoxTemplate");
	codeBox:SetPoint("TOPLEFT", "LKC_GUI_SECRET1", "TOPLEFT", 0, -30);
	LKC_GUI_SetEditBoxDetails(codeBox);
	codeBox:SetSize(120, 32);
	LKC_GUI_AddDescLeft(codeBox, "Other Codes -");

	local codeBox = CreateFrame("EditBox", "LKC_GUI_SECRET3", LKC_GUI, "InputBoxTemplate");
	codeBox:SetPoint("TOPLEFT", "LKC_GUI_SECRET2", "TOPLEFT", 130, 0);
	LKC_GUI_SetEditBoxDetails(codeBox);
	codeBox:SetSize(120, 32);

	-- Save Button
	local LKC_GUI_SaveButton = CreateFrame("Button", "LKC_GUI_SaveButton", LKC_GUI, "UIPanelButtonTemplate");
	LKC_GUI_SaveButton:SetWidth(150);
	LKC_GUI_SaveButton:SetHeight(22);
	LKC_GUI_SaveButton:SetText(SAVE);
	LKC_GUI_SaveButton:RegisterForClicks("LeftButtonUp");
	LKC_GUI_SaveButton:ClearAllPoints();
	LKC_GUI_SaveButton:SetPoint("TOPLEFT", LKC_GUI, "BOTTOMLEFT", 20, 40);
	LKC_GUI_SaveButton:SetScript("OnClick", LKC_GUI_SaveButtonClick);

	-- Set an "OnShow" script for the GUI screen to fill in the config data.
	LKC_GUI:SetScript("OnShow", LKC_GUI_FillData);
end
